﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SHDocVw;
using MSHTML;
using System.Data;

/*
 * 打开浏览器的线程，根据参数实现，来路，打开，停留，动作，点广告等动作，模拟鼠标等动作
 */
namespace cn.loozo.BrokenArrow
{
    class ThreadBrowser
    {
        private ManualEvent _doneEvent;
        private Website _website;
    
        public ThreadBrowser(ManualEvent doneEvent , Website ws)
        {
            _doneEvent = doneEvent;
            _website = ws;
        }

        // Wrapper method for use with thread pool.
        public void ThreadPoolCallback(Object threadContext)
        {
            int threadIndex = (int)threadContext;

            // EventHandlers eve = new EventHandlers();
            SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
            object Empty = 0;
            IE.Visible = true;
            if (_website.referrer != "")
            {
                object URL = _website.referrer;
                   
                IE.Navigate2(ref URL, ref Empty, ref Empty, ref Empty, ref Empty);

                while (IE.Busy || IE.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                {
                    //System.Threading.Thread.Sleep(1000);
                    Application.DoEvents();
                }
            }
            System.Threading.Thread.Sleep(3000);
            IHTMLDocument2 HTMLDocument = (IHTMLDocument2)IE.Document;
            IHTMLElementCollection  links = HTMLDocument.links; 
            IHTMLAnchorElement target = null;
            foreach (IHTMLAnchorElement el in links)
            {
                if (el.href.IndexOf(_website.link) > -1) {
                    target = el; 
                    break;
                }
            }

            if (target != null) {
                target.target = "_self";
                ((IHTMLElement)target).click();
                System.Threading.Thread.Sleep(1000); 
                while (IE.Busy || IE.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                {
                    //System.Threading.Thread.Sleep(1000);
                    Application.DoEvents();
                }
                
            }
            System.Threading.Thread.Sleep(3000); 
            IE.Quit();
            _doneEvent.stat = true;
        }
         
    }
    public class ManualEvent
    {
        public bool stat
        {
            get { return _n; }
            set { _n = value; }
        }
        private bool _n = false;

    }
    class EventHandlers
    {
        public void OnBeforeNavigate2(object sender, ref object URL,
                                      ref object Flags, ref object Target,
                                      ref object PostData, ref object Headers,
                                      ref bool Cancel)
        {
            Console.WriteLine("BeforeNavigate2 fired!");
        }
    }
}
